<?php

function showError(string $msg = '操作失败！', array $data = [])
{
    return [
        'status' => 'error',
        'msg'    => $msg,
        'data'   => $data,
    ];
}

function showSuccess(string $msg = '操作成功！', array $data = [])
{
    return response()->json([
        'status' => 'success',
        'msg'    => $msg,
        'data'   => $data,
    ]);
}

function modifyEnv(array $data)
{
    $envPath = base_path() . DIRECTORY_SEPARATOR . '.env';

    $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES));
    $contentArray->transform(function ($item) use ($data) {
        foreach ($data as $key => $value) {
            if (str_contains($item, $key)) {
                return $key . '=' . $value;
            }
        }
        return $item;
    });
    $content = implode($contentArray->toArray(), "\n");

    \File::put($envPath, $content);
}

